package sunwin.yoms.web.controller.system;

import jodd.datetime.JDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import sunwin.yoms.comm.YomsException;
import sunwin.yoms.comm.YomsFunction;
import sunwin.yoms.comm.YomsPage;
import sunwin.yoms.comm.model.NameAndValueModel;
import sunwin.yoms.domain.comm.AdminOperateDomain;
import sunwin.yoms.service.system.LogService;
import sunwin.yoms.web.comm.R;
import sunwin.yoms.web.comm.YomsController;
import sunwin.yoms.web.comm.resolverArg.YomsDateParam;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * Created by ocmt on 2015-8-29.
 */
@Controller
public class LogController {

    @Autowired
    private LogService logService;
    private final String FindLog = "system/log.find";

    /**
     * 查找日志
     * @param domain         日志domain
     * @param subregionIndex 日志分区index
     * @param pageIndex      页码
     * @param startTime      起始时间
     * @param endTime        截至时间
     * @return
     * @throws YomsException
     */
    @RequestMapping(value = R.FindLog, method = RequestMethod.GET)
    @YomsController(functionNo = YomsFunction.FindLog, viewName = FindLog)
    public ModelAndView findBlogroll(AdminOperateDomain domain,
                                     @RequestParam(required = false, defaultValue = "-1") int subregionIndex,
                                     @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @YomsDateParam JDateTime startTime,
                                     @YomsDateParam JDateTime endTime,
                                     ModelMap modelMap) throws Exception {
        modelMap.addAttribute("domains", domain);
        modelMap.addAttribute("startTime", startTime == null ? null : startTime.toString("YYYY-MM-DD hh:mm"));
        modelMap.addAttribute("endTime", endTime == null ? null : endTime.toString("YYYY-MM-DD hh:mm"));
        modelMap.addAttribute("subregionIndex",subregionIndex);
        buildSubregion(modelMap);

        //转换时间
        Date date1 = startTime == null ? null : startTime.convertToDate();
        Date date2 = endTime == null ? null : endTime.convertToDate();

        //设置分页
        YomsPage yomsPage=new YomsPage();
        yomsPage.setPageIndex(pageIndex);
        yomsPage.setPageSize(50);

        //获取默认分区index
        if(subregionIndex == -1){
            subregionIndex = logService.getDefaultSubregion().getSubregionIndex();
        }

        //获取日志记录
        Collection<AdminOperateDomain> domains=logService.getPageLogs(domain,yomsPage,subregionIndex,date1,date2);
        ModelAndView mav = new ModelAndView();
        mav.addObject("yomsPage", yomsPage);
        mav.addObject("logs",domains);
        return mav;
    }

    /**
     * 构造分区
     * @param modelMap
     * @throws Exception
     */
    private void buildSubregion(ModelMap modelMap) throws Exception {
        int subregionTotal = logService.getDefaultSubregion().getSubregionTotal();
        List<NameAndValueModel> totals = new ArrayList<NameAndValueModel>();
        NameAndValueModel nv = new NameAndValueModel();
        nv.setName("日志分区");
        nv.setValue(String.valueOf(-1));
        totals.add(nv);
        for (int i = subregionTotal; i > 0; i--) {
            nv = new NameAndValueModel();
            nv.setValue(String.valueOf(i - 1));
            totals.add(nv);
        }
        for (int i = 0; i < totals.size(); i++) {
            if(i > 0){
                //%s表示输出字符串
                totals.get(i).setName(String.format("第%s分区", i));
            }
        }
        modelMap.addAttribute("subregionTotals", totals);
    }
}
